<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
</HEAD>
<BODY>
<H1>StackOverflowException</H1>
<H2>What it means</H2>
<P>The system ran out of space in the stack, which is the region of memory that 
holds procedure arguments and local variables.&nbsp; This means there have been 
"too many" procedure calls in the program and that almost always means that the 
program has a bug.</P>
<H2>What causes it</H2>
<P>Usually either:</P>
<UL>
  <LI>Some procedure kept calling itself over and over without ever returning, 
  or</LI>
  <LI>Some set of procedures kept calling each other without any of them ever 
  returning.</LI></UL>
<P>This is known as an "infinite recursion".</P>
<P>In some rare cases, the system really does need that much memory.&nbsp; For 
example, if you have a recusive program and you give it an input that is 
"unusally large" (the sense of what constitutes large varies from program to 
program, though) and so it requires an unusually "deep" recursion.</P>
<H2>What to do</H2>
<P>The thing to remember is that most programs with this error are recusive 
programs, meaning that they look something like:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE><FONT face=Arial>[define <STRONG>procedure</STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [arg<SUB>1</SUB> ... arg<SUB>n</SUB> →<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[if <FONT face="Times New Roman">... <EM>check if it's a simple problem </EM>...<BR></FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT face="Times New Roman">... <EM>special code to handle simple problems </EM>...</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [procedure <FONT face="Times New Roman">... <EM>compute the arguments for a simpler version of the problem</EM> ...</FONT>]]]]</FONT></PRE></BLOCKQUOTE>
<P>This is called "simple recursion".&nbsp; The code above is only schematic to 
give you an idea of what's going on.&nbsp; Your code might be more 
complicated.&nbsp; For example, this procedure just takes the return value it 
gets from calling itself and returns it.&nbsp; But it might also alter it 
somehow, as in:&nbsp;</P>
<P></P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE><FONT face=Arial>[define <STRONG>procedure</STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [arg<SUB>1</SUB> ... arg<SUB>n</SUB> →<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[if <FONT face="Times New Roman">... <EM>check if it's a simple problem </EM>...<BR></FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT face="Times New Roman">... <EM>special code to handle simple problems </EM>...</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [+ [procedure <FONT face="Times New Roman">... <EM>compute the arguments for a simpler version of the problem</EM> ...</FONT>]<BR>                           1]]]</FONT></PRE></BLOCKQUOTE>
<P>Or it might have several <FONT face=Arial>if</FONT>s for different kinds of 
simple problems or different ways it should call itself.&nbsp; Finally, it might 
have several different procedures that call each other, as in:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"><PRE><FONT face=Arial>[define <STRONG>procedure1</STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [arg<SUB>1</SUB> ... arg<SUB>n</SUB> →<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[if <FONT face="Times New Roman">... <EM>check if it's a simple problem </EM>...<BR></FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT face="Times New Roman">... <EM>special code to handle simple problems </EM>...</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [procedure1 <FONT face="Times New Roman">... <EM>compute the arguments for a simpler version of the problem</EM> ...</FONT>]]]]<BR></FONT><FONT face=Arial><BR><BR><BR><BR><FONT face=Arial>[define <STRONG>procedure2</STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [arg<SUB>1</SUB> ... arg<SUB>n</SUB> →<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[if <FONT face="Times New Roman">... <EM>check if it's a simple problem </EM>...<BR></FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT face="Times New Roman">... <EM>special code to handle simple problems </EM>...</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [procedure2 <FONT face="Times New Roman">... <EM>compute the arguments for a simpler version of the problem</EM> ...</FONT>]]]]</FONT></FONT></PRE></BLOCKQUOTE>
<P>(this is called "mutual recursion").&nbsp; </P>
<P>What matters is that the code is making some decision about whether to call 
itself.&nbsp; It either calls itself with a "simpler" or "easier" version of its 
arguments (smaller sequence, smaller number, whatever) and then fixes up the 
result to turn it into an answer to the original problem, or it 
<EM>terminates</EM> the recursion by noticing it's arrived at a version of the 
problem that's simple enough that it can solve it directly.</P>
<P>Okay.&nbsp; That our review of what recursion is.&nbsp; If you got a stack 
overflow, you probably had an infinite recursion and that means your program 
probably has one of the following bugs:</P>
<UL>
  <LI><STRONG>The termination check (the if) is buggy somehow</STRONG><BR>You 
  can figure this out by looking at the stack in the debugger and seeing what 
  arguments are being passed to the procedure(s) that are recursing and looking 
  to see if there was&nbsp;a point where it should have terminated the recursion 
  but kept on going instead.</LI>
  <LI><STRONG>The termination check is missing entirely<BR></STRONG>Oops.&nbsp; 
  It happens to the best of us.</LI>
  <LI><STRONG>The code that's supposed to compute the arguments for the 
  "simpler" version of the problem is somehow buggy<BR></STRONG>Again, you can 
  figure this out by using the debugger to look at the arguments that were 
  passed to the relevant procedures to see if they're "shrinking" the way they 
  should be.</LI></UL>
<P>So your job in debugging the infinite recursion is to:</P>
<OL>
  <LI>Look at the stack in the debugger to find out what procedure(s) are 
  running out of control.</LI>
  <LI>Look at their arguments for obvious evidence of either failing to 
  terminate when they should or failing to simplify the arguments in the 
  recursive call.</LI>
  <LI>Look at the souce code to find the relevant if or recursive call and fix 
  it.</LI></OL>



</BODY>
</HTML>
